package com.vq.controller;

import com.vq.common.Code;
import com.vq.common.R;
import com.vq.entity.User;
import com.vq.service.TrendsService;
import com.vq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    HttpSession session;

    @Autowired
    UserService userService;
    @Autowired
    TrendsService trendsService;

    /**
     * 通过账号获取账户信息（不包含密码）
     * @param account
     * @return
     */
    @GetMapping("/{account}")
    public R<User> selectByAccount(@PathVariable String account) {
        User user = userService.selectByAccount(account, false);
        R<User> r = new R<>(0);
        if(user!=null) {
            r.setCode(1);
            r.setData(user);
        }
        return r;
    }
    /**
     * 通过用户名搜素用户
     * @param username
     * @return
     */
    @GetMapping()
    public R searchByName(@RequestParam("username") String username) {
        List<User> userList = userService.search(username);
        return new R(1, userList);
    }

    /**
     * 修改用户名称
     * @param user
     * @return
     */
    @PutMapping("/1")
    @ResponseBody
    public R<User> updateUsername(@RequestBody User user) {
        User myUser = (User) session.getAttribute("myUser");
        R<User> r = new R<>(0, user);
        //确保是用户自己操作
        if(!myUser.getAccount().equals(user.getAccount())) return r;
        //如果名字相同就不进行修改
        if (myUser.getUsername().equals(user.getUsername())) {
            r.setCode(1);
            return r;
        }
        user.setId(myUser.getId());
        boolean bo = userService.updateById(user);
        //用户修改成功
        if(bo) {
            //修改用户名称，并且修改动态名称
            bo = trendsService.updateToUsernameByAccount(user.getAccount(), user.getUsername());
            //全部修改成功，重新从数据库获取用户信息并且刷新session域信息
            if(bo) {
                r.setCode(1);
                myUser = userService.selectById(myUser.getId(), false);
                session.setAttribute("myUser", myUser);
            }
        }
        r.setData(user);
        //记录日志
        log.info("修改用户名称："+myUser);
        return r;
    }
    /**
     * 修改用户密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PutMapping("/2")
    @ResponseBody
    public R<User> updatePassword(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) {
        User user = (User) session.getAttribute("myUser");
        R<User> r = new R<>(0, user);
        if(user == null) return r;
        //保证新旧密码不为空
        if (oldPassword==null||newPassword==null||"".equals(oldPassword)||"".equals(newPassword)) {
            log.info("修改用户密码：{id="+user.getId()+", oldPassword="+oldPassword+", newPassword="+newPassword+"}");
            return r;
        }
        //密码格式错误
        if (!newPassword.matches("^(?=.*[a-zA-Z])(?=.*\\d).{6,20}$")) {
            return new R<>(Code.PASSWORD_FORMAT_NO);
        }
        //修改密码
        boolean bo = userService.updateToPasswordById(user.getId(), oldPassword, newPassword, false);
        //判定是否修改成功
        if (bo) {
            user.setPassword(newPassword);
            r.setCode(1);
        }
        //记录日志
        log.info("修改用户密码：{id="+user.getId()+", oldPassword="+oldPassword+", newPassword="+newPassword+"}");
        return r;
    }
}
